package com.jungle.app.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Arrays;

import com.jungle.common.utils.poi.ExcelUtil;
import com.pig4cloud.plugin.excel.annotation.RequestExcel;
import lombok.RequiredArgsConstructor;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.jungle.common.annotation.RepeatSubmit;
import com.jungle.common.core.controller.BaseController;
import com.jungle.common.core.domain.PageQuery;
import com.jungle.common.core.domain.R;
import com.jungle.common.core.validate.AddGroup;
import com.jungle.common.core.validate.EditGroup;
import com.jungle.app.domain.vo.ImgLinkDataVo;
import com.jungle.app.domain.bo.ImgLinkDataBo;
import com.jungle.app.service.IImgLinkDataService;
import com.jungle.common.core.page.TableDataInfo;

/**
 * 图片链接数据
 *
 * @author jungle
 * @date 2023-09-28
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/smart/linkData")
@Slf4j
public class ImgLinkDataController extends BaseController {

    private final IImgLinkDataService iImgLinkDataService;

    /**
     * 查询图片链接数据列表
     */
    @GetMapping("/list")
    public TableDataInfo<ImgLinkDataVo> list(ImgLinkDataBo bo, PageQuery pageQuery) {
        log.info("查询图片信息：{}\n{}",bo,pageQuery);
        return iImgLinkDataService.queryPageList(bo, pageQuery);
    }
    /**
     * 导入图片链接数据列表
     */
    @PostMapping("/import")
    public R<Long> importExcel(@RequestExcel List<ImgLinkDataBo> list){
        return R.ok(list.stream().map(iImgLinkDataService::insertByBo).filter(v->v==true).count());
    }
    /**
     * 导出图片链接数据列表
     */
    @PostMapping("/export")
    public void exportExcel(ImgLinkDataBo bo, HttpServletResponse response) throws IOException {
        List<ImgLinkDataVo> data = iImgLinkDataService.exportExcel(bo, response);
        ExcelUtil.exportExcel(data,"图片信息",ImgLinkDataVo.class,response);
    }


    /**
     * 获取图片链接数据详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<ImgLinkDataVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String id) {
        return R.ok(iImgLinkDataService.queryById(id));
    }

    /**
     * 新增图片链接数据
     */
    @RepeatSubmit()
    @PostMapping(/*consumes = MediaType.MULTIPART_FORM_DATA_VALUE*/)
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ImgLinkDataBo bo) {
        return toAjax(iImgLinkDataService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改图片链接数据
     */
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ImgLinkDataBo bo) {
        return toAjax(iImgLinkDataService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除图片链接数据
     *
     * @param ids 主键串
     */
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iImgLinkDataService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }



}
